package com.pig4cloud.pig.admin.task;

import cn.hutool.core.date.DateUtil;
import com.pig4cloud.pig.admin.api.entity.NewsList;
import com.pig4cloud.pig.admin.constant.CommonConstant;
import com.pig4cloud.pig.admin.task.base.BaseTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadLocalRandom;

@Component
public class Program51CTONews extends BaseTask {

	private String websiteUrl = "https://www.51cto.com/developer/p";

	@Scheduled(cron = "0 0/5 * * * ?")
	public void doTask(){
		int randomPage = ThreadLocalRandom.current().nextInt(1, 1535);
		String request = httpUtilService.getRequest(1,this.getClass(),websiteUrl+randomPage, CommonConstant.UTF8);
		Document parse = Jsoup.parse(request);
		Elements select = parse.body().select(".components-topic-list-content > div");
		for(Element ele : select){
			String imgUrl = ele.select("img").attr("src");
			String link = ele.select(".article-irl-ct > a").attr("href");
			String topic = ele.select(".article-irl-ct > a").text();
			String desc = ele.select(".title-box > a").text();
			String time = ele.select(".article-irl-cb_time").text();
			NewsList news = NewsList.create(idGenerator.nextId());
			news.setSubTitle(desc);
			news.setImgUrl(imgUrl);
			news.setUrl(link);
			news.setCategory("编程");
			//news.setUrlHash(link.hashCode());
			if(time.length()>19){
				setTitleAndTime(link,news);
			}else{
				news.setCreateTime(DateUtil.parse(time));
				news.setTitle(topic);
			}
			pushNewsToRedis(news);
		}
	}


	public void setTitleAndTime(String url,NewsList news){
		String request = httpUtilService.getRequest(1,this.getClass(),url, CommonConstant.UTF8);
		Element body = Jsoup.parse(request).body();
		String topic = body.select("h1").text();
		String time = body.select("time").text();
		news.setTitle(topic);
		news.setCreateTime(DateUtil.parse(time));
    }
}
